/**
 * 
 */
package org.sgt.datamodel.editor.action;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.actions.ActionDelegate;
import org.sgt.datamodel.Action;
import org.sgt.datamodel.util.ActionHelper;

/**
 * @author BUTTIGHOFFER-A
 *
 */
public class ExecuteAction extends ActionDelegate implements IActionDelegate {

	private Action executableAction;

	/**
	 * 
	 */
	public ExecuteAction() {
		super();
	}

	@Override
	public void run(IAction action) {
		ActionHelper.execute(executableAction);
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object object = ((IStructuredSelection) selection)
					.getFirstElement();
			if (object instanceof Action) {
				executableAction = (Action) object;

				action.setEnabled(true);
				return;
			}
		}
		executableAction = null;
		action.setEnabled(false);
	}

}
